<template>
  <div class="permission">
    <template>
      <div class="permission-container">
        <div class="app-container">
          <!-- 表格 -->
          <el-card>
            <div style="text-align: right; margin-bottom: 20px">
              <el-button type="primary" size="small" @click="handleAll(0,1)">添加权限</el-button>
            </div>
            <el-table border :data="list" row-key="id">
              <el-table-column label="名称" prop="name" />
              <el-table-column label="标识" prop="code" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{row}">
                  <el-button v-if="row.type===1" size="small" type="text" @click="handleAll(row.id,2)">添加权限点</el-button>
                  <el-button size="small" type="text" @click="edit(row.id)">查看</el-button>
                  <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-card>
        </div>
      </div>
    </template>
    <!-- 新增编辑的弹层 -->
    <el-dialog :visible="showDialog" :title="title" @close="showDialog = false">
      <!-- 表单内容 -->
      <el-form label-width="100px">
        <el-form-item label="权限名称">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用">
          <el-switch
            v-model="formData.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="1"
            inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { reqGetPermissionList, reqAddPermission, reqDelPermission, reqGetPermissionDetail, reqUpdatePermission } from '@/api/permisson'
import { reanListToTreeData } from '@/utils/departments'
export default {
  name: 'Permission',
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    }
  },
  computed: {
    title() {
      if (this.formData.id) {
        return '查看编辑'
      } else {
        return '添加权限'
      }
    }
  },
  created() {
    this.getAllList()
  },
  methods: {
    // 关闭弹框并清空表单
    handleClose() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 数据初始化
    async  getAllList() {
      const { data } = await reqGetPermissionList()
      // console.log(data)
      // 权限，树形结构==》一级页面访问权限pid是'0'
      this.list = reanListToTreeData(data, '0')
    },
    // 判断是一级弹框还是二级
    handleAll(pid, type) {
      this.showDialog = true
      // console.log(pid, type)
      this.formData.type = type
      this.formData.pid = pid
    },
    // 点击确定新增
    async handleSubmit() {
      // 判断是编辑还是添加
      if (this.formData.id) {
        // 编辑
        await reqUpdatePermission(this.formData)
      } else {
        // 添加
        await reqAddPermission(this.formData)
      }
      this.handleClose()
      this.getAllList()// 重新调取数据
    },
    // 点击删除
    del(id) {
      this.$confirm('确认删除嘛').then(async() => {
        await reqDelPermission(id)
        this.getAllList()
      }).catch(() => {
        console.log('取消')
      })
    },
    async edit(id) {
      this.showDialog = true
      const { data } = await reqGetPermissionDetail(id)
      this.formData = data
    }
  }
}
</script>

<style>

</style>
